<?php

	require_once("dbconnect.php");
	require_once("ekgames.php");
	
	ob_end_clean(); //Clear all choed & printed item	
	
	//Receive then respond the request
	$requestFunction = receiveRequest("action");

	switch ($requestFunction) {
		case "loadusertower":
			echo loadUserBarricade();
			break;
		case "loadtower":
			echo loadTower();
			break;
		
		default:
			echo "ERR#01: Bad request!";
	}
	
	mysql_close($con);
	
	//=======================================================================================
	
	//Load all user's card
	function loadUserTower () {
		$userid = receiveRequest("userid");
		
		if($userid == null) {
			badRequest();
			return;
		}
		
		$sql = "SELECT * FROM utowers WHERE USER_ID='$userid'";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "<root>";
		while($r = mysql_fetch_array($q)){
			$xml .= "<tower>";
			$xml .= "<NUM>".$r['NUM']."</NUM>";
			$xml .= "<TOWER_ID>".$r['TOWER_ID']."</TOWER_ID>";  
			$xml .= "</tower>";
		}
		$xml .= "</root>";
		
		return $xml;
	}
	
	//Load user's card by card id
	function loadTower () {
		$towerid = receiveRequest("towerid");
		
		if($towerid == null) {
			badRequest();
			return;
		}
			
		$sql = "SELECT * FROM towers WHERE ID='$towerid'";
		$q	 = mysql_query($sql) or die(mysql_error());
		
		$xml = "<root>";
		while($r = mysql_fetch_array($q)){
			$xml .= "<tower>";
			$xml .= "<ID>".$r['ID']."</ID>";  
			$xml .= "<NAME>".$r['NAME']."</NAME>";
			$xml .= "<HP>".$r['HP']."</HP>";
			$xml .= "<MANA>".$r['MANA']."</MANA>";
			$xml .= "<SIZE>".$r['SIZE']."</SIZE>";
			$xml .= "<PIVOT>".$r['PIVOT']."</PIVOT>";
			$xml .= "<IMG>".$r['IMG']."</IMG>";
			$xml .= "<MODEL>".$r['MODEL']."</MODEL>";
			$xml .= "</tower>";
		}
		$xml .= "</root>";
		
		return $xml;
	}
?>